package cn.ywang.pattern.adapter.passport;

import cn.ywang.pattern.adapter.LoginService;
import cn.ywang.pattern.adapter.ResultMsg;
import cn.ywang.pattern.adapter.passport.adapter.LoginAdapter;
import cn.ywang.pattern.adapter.passport.adapter.LoginForQQAdapter;
import cn.ywang.pattern.adapter.passport.adapter.LoginForTokenAdapter;
import cn.ywang.pattern.adapter.passport.adapter.LoginForWeChatAdapter;

/**
 * 通行证适配器
 *
 * @author BeatificWang
 * @date 2019/3/19 23:35
 */
public class PassportAdapter extends LoginService implements Passport {
	/**
	 * @param openId
	 * @return
	 */
	@Override
	public ResultMsg loginForQQ(String openId) {
		return this.processLogin(openId, LoginForQQAdapter.class);
	}

	/**
	 * @param openId
	 * @return
	 */
	@Override
	public ResultMsg loginForWeChat(String openId) {
		return this.processLogin(openId, LoginForWeChatAdapter.class);
	}

	/**
	 * @param toKen
	 * @return
	 */
	@Override
	public ResultMsg loginForToken(String toKen) {
		return this.processLogin(toKen, LoginForTokenAdapter.class);
	}

	/**
	 * @param mobile
	 * @param code
	 * @return
	 */
	@Override
	public ResultMsg loginForMobile(String mobile, String code) {
		return null;
	}

	/**
	 * @param username
	 * @param password
	 * @return
	 */
	@Override
	public ResultMsg loginForRegister(String username, String password) {
		return null;
	}

	public ResultMsg processLogin(String key, Class<? extends LoginAdapter> clazz) {
		try {
			LoginAdapter adapter = clazz.newInstance();
			if (adapter.support(adapter)) {
				return adapter.login(key, adapter);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
}
